/**
 * @(#) PacdpProxy.as
 *
 * @date		2012. 09. 19.
 * @version		1.0
 * @author		Park, Jutae
 * @description	 CDP신청 Proxy - Proxy
 * 				CDP 신청 리스트를 조회하고 저장, 수정, 삭제한다.
 */
package com.skt.common_hr.sepcost.model
{
	import com.skt.common_hr.common.puremvc.CommonProxy;
	import com.skt.common_hr.sepcost.SepCostFacade;
	import com.skt.common_hr.sepcost.model.vo.SepCostSaveVO;
	import com.skt.common_hr.sepcost.model.vo.SepCostVO;
	import com.wcl.dataloader.*;
	import com.wcl.utils.DateUtil;
	
	import mx.collections.ArrayCollection;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
		
	public class SepCostProxy extends CommonProxy
	{
		public static const NAME:String = "InrecProxy";
		public static const LOAD_STATEMENT_URL_COMPLTE:String		= "loadStatementUrlComplete";
		public static const LOAD_STATEMENT_URL_FAILED:String		= "loadStatementUrlFailed";
		
		public var inrecList:ArrayCollection;
		public var sinChungList:ArrayCollection;
		public var acMedYear:ArrayCollection;
		public var sRettext:String;
		public var readList:ArrayCollection;
		public var causeList:ArrayCollection;
		public var errCode:int;
		public var errText:String;
		public var url:String;
		public var e_joint:Date;
		
		public function SepCostProxy(data:Object=null)
		{
			super(NAME, data);
		}
		
		public function loadCheck(_pernr:String):void
		{
			var params:Object = new Object();
			params["I_PERNR"] = _pernr;
			params["I_DATUM"] = DateUtil.today();
			loadData("Z_HR_NT07", params, loadCheckResult, loadFault); 
		}
		
		public function load(pernr:String, sDay:Date, eDay:Date):void
		{
			var params:Object = new Object();
			params["I_PERNR"] = pernr;
			params["I_BEGDA"] = DateUtil.toBasicString(sDay);
			params["I_ENDDA"] = DateUtil.toBasicString(eDay);
			
			loadData("Z_HR_NT05", params, loadResult, loadFault); 
		}
		
		public function insert(rec:SepCostVO):void
		{
			
			var params:Object = new Object();
			params["I_ACTIO"] = "SAVE"; 			
			var obj:Object = new Object();
			var tables:Object = new Object();
			var table:Array = new Array();
			
			obj["PERNR"]				= rec.PERNR;
			obj["BEGDA"]				= DateUtil.toBasicString(rec.BEGDA);
			obj["ENDDA"]				= DateUtil.toBasicString(rec.ENDDA);
			obj["DATUM"]				= DateUtil.toBasicString(rec.DATUM);
			obj["SPAMT"]				= rec.SPAMT;
			obj["TELNO"]				= rec.TELNO;
			table.push(obj);
			
			tables["T_IN"] = table;

			saveData("Z_HR_NT06", params, tables, insertResult, insertFault);
		}

		public function del(rec:SepCostVO):void
		{ 
			var params:Object = new Object();
			params["I_ACTIO"] = "DEL"; 			
			var obj:Object = new Object();
			var tables:Object = new Object();
			var table:Array = new Array();
			
			obj["PERNR"]				= rec.PERNR;
			obj["BEGDA"]				= DateUtil.toBasicString(rec.BEGDA);
			obj["ENDDA"]				= DateUtil.toBasicString(rec.ENDDA);
			obj["DATUM"]				= DateUtil.toBasicString(rec.DATUM);
			obj["GJAHR"]				= rec.GJAHR;
			obj["PERIO"]				= rec.PERIO;
			obj["REASN"]				= rec.REASN;
			obj["SPAMT"]				= rec.SPAMT;
			obj["STATS"]				= rec.STATS;
			obj["STATX"]				= rec.STATX;
			obj["TELNO"]				= rec.TELNO;
			table.push(obj);
			
			tables["T_IN"] = table;

			saveData("Z_HR_NT06", params, tables, delResult, delFault);
		}
		
		public function loadResult(event:ResultEvent):void
		{
			if(event.result["RETCODE"]==0) {  
				inrecList = parseInrecList(event.result.T_OUT);
				e_joint = DateUtil.stringToDateTime(event.result.E_JOINT);
				facade.sendNotification(SepCostFacade.LOAD_LIST_COMPLETE, this);
			}
			else {
				facade.sendNotification(SepCostFacade.LOAD_LIST_FAILED, this);
			}
		}
		
		private function fault(event:FaultEvent):void
		{
			trace(event.fault.faultString);
			errCode = -1;
			errText = "호출실패";
			url = "";
			facade.sendNotification(SepCostProxy.LOAD_STATEMENT_URL_FAILED, this);
		}
		
		private function loadFault(event:FaultEvent):void
		{
			facade.sendNotification(SepCostFacade.LOAD_LIST_FAILED, this);
		}
		
		public var failedAlert:String; 
		public function insertResult(event:ResultEvent):void
		{
			if(event.result["RETCODE"]==0) {  
				readList = parseInrecList(event.result.T_IN);
				facade.sendNotification(SepCostFacade.INSERT_COMPLETE, this);
			}
			else {
				failedAlert = event.result.RETTEXT;
				facade.sendNotification(SepCostFacade.CANCEL_REQUEST_INPUT, this);
			}
		}
		
		private function insertFault(event:FaultEvent):void
		{
			facade.sendNotification(SepCostFacade.INSERT_FAILED, this);
		}
		
		public var updateFailed:String; 
		public function updateResult(event:ResultEvent):void
		{
			if(event.result["RETCODE"]==0) {   
				facade.sendNotification(SepCostFacade.OK_REQUEST_INPUT, this);
			}
			else {
				updateFailed = event.result.RETTEXT; 
				facade.sendNotification(SepCostFacade.CANCEL_REQUEST_INPUT, this);
			}
		}

		public function delResult(event:ResultEvent):void
		{
			if(event.result["RETCODE"]==0) {  
				facade.sendNotification(SepCostFacade.DELETE_COMPLETE, this);
			}
			else {
				facade.sendNotification(SepCostFacade.DELETE_FAILED, this);
			}
		}
		
		private function delFault(event:FaultEvent):void
		{
			facade.sendNotification(SepCostFacade.DELETE_FAILED, this);
		}
		
		private function parseInrecList(srcArr:ArrayCollection):ArrayCollection
		{
			var retArr:ArrayCollection = new ArrayCollection();
			
			for each(var rec:Object in srcArr) 
			{
				var vo:SepCostVO = new SepCostVO();
				vo.BEGDA		= DateUtil.stringToDateTime(rec["BEGDA"]);
				vo.ENDDA		= DateUtil.stringToDateTime(rec["ENDDA"]);
				vo.DATUM		= DateUtil.stringToDateTime(rec["DATUM"]);
				vo.GJAHR		= rec["GJAHR"];
				vo.PERIO		= rec["PERIO"];
				vo.PERNR 		= rec["PERNR"];
				vo.REASN		= rec["REASN"];
				vo.SPAMT 		= rec["SPAMT"];
				vo.STATS		= rec["STATS"];
				vo.STATX		= rec["STATX"];
				vo.TELNO	 	= rec["TELNO"];
				
				// 변경삭제 유무 
				if(vo.STATS == '1'){
					vo.STATS_F = true;
				} else {
					vo.STATS_F = false;
				}	
				
				retArr.addItem(vo);
			}
			return retArr;
		}
		
		public var failedCheckAlert:String;
		public function loadCheckResult(event:ResultEvent):void
		{
			if(event.result["RETCODE"]==0) {  
				facade.sendNotification(SepCostFacade.LOAD_CHECK_COMPLETE, this);
			}
			else {
				failedCheckAlert = event.result.RETTEXT;
				facade.sendNotification(SepCostFacade.LOAD_CHECK_FAILED, this);
			}
		}
				
	}
}